import jofc2.model.Chart
import jofc2.model.elements.LineChart
import jofc2.model.axis.YAxis
import jofc2.model.axis.XAxis
import jofc2.model.axis.Label
import jofc2.model.elements.AreaHollowChart
import jofc2.model.elements.PieChart
import jofc2.model.axis.Label.Rotation
import jofc2.model.elements.BarChart
import jofc2.OFC
import jofc2.model.elements.FilledBarChart
import jofc2.model.elements.SketchBarChart
import jofc2.model.elements.HorizontalBarChart
import jofc2.model.elements.ScatterChart
import java.math.MathContext
import jofc2.model.elements.StackedBarChart
import jofc2.model.elements.StackedBarChart.StackValue

class TeacherAttendanceMgrController {
    // 教师列表
    def list = {
        if (session.school) { 
            def month = Calendar.instance.get(Calendar.MONTH)
            log.info "TeacherAttendanceMgr.list month = ${month}"
            def months = 0..11
            println 12+month-5
            def recently = (month>5) ? months[month..month-5] : months[month..0,11..(11+month-5)]  

            [teacherInstanceList: Teacher.findAllBySchoolAndState(session.school , false) ,
                teacherInstanceTotal:Teacher.countBySchoolAndState(session.school , false),
                    recently : recently]
        }else{
            [teacherInstanceList: [] ,
                teacherInstanceTotal:0  ]
        }
    }

    // 选择教师某月的考勤
    def attendance = { 
        [id: params.id , month : params.month]
    }

    def attendanceChart = { 
        log.info "TeacherAttendanceMgrController.attendanceChart month and id = ${params.month} ${params.id}"
        def teacher = Teacher.get(params.id)
        if (teacher) {
      
            def calendar = Calendar.getInstance()
            calendar.set(Calendar.MONTH ,  params.month.toInteger() );
            calendar.set(Calendar.DAY_OF_MONTH ,  1 );
            def first = calendar.getTime()
            calendar.add(Calendar.MONTH ,  1);
            def last = calendar.getTime() 

//            log.info "TeacherAttendanceMgr.attendance first and last = ${first}/${last}"
//            return [teacherAttendanceInstanceList : TeacherAttendance.withCriteria {
//                and{
//                    eq('teacher' , teacher) 
//                    between('attendanceTime' , first ,  last)  
//                }  
//                order('attendanceTime' , 'asc');
//            }] 
            def c = new Chart("${new Date().format('yyyy年MM月')} ${teacher}的考勤纪录").setYAxis(new YAxis().setRange(8, 10, 1));

            def xaxis = new XAxis().setTickHeight(5)
            
            def l1 = new LineChart(LineChart.Style.HOLLOW);  
            l1.setHaloSize(0).setWidth(2).setDotSize(4).setTooltip("#val#到校");

            calendar.set(Calendar.MONTH ,  params.month.toInteger() );
            calendar.set(Calendar.DAY_OF_MONTH ,  1 );
            def simpledateformat = new java.text.SimpleDateFormat("dd日\nE")
            while(true){
                
                if (calendar.get(Calendar.DAY_OF_WEEK )!= Calendar.SATURDAY &&
                    calendar.get(Calendar.DAY_OF_WEEK )!= Calendar.SUNDAY ) {
                        def value = 8.5 + Math.round((Math.random()*60))/100 
                        if (value > 9) {
                            l1.addDots(new LineChart.Dot(value, "#FF9999", 5, 2))  
                        }else{
                            l1.addValues(new Float(value))
                        }
                        
                        xaxis.addLabels(new Label(simpledateformat.format(calendar.getTime())))
                }
                calendar.add(Calendar.DAY_OF_YEAR , 1)
                if (calendar.get(Calendar.MONTH) != params.month.toInteger() ) {
                    break
                }
            }
 
            c.setXAxis(xaxis);  
            c.addElements(l1 );

            //        println c.toDebugString()
            render c;
        }else{
             []
        }
    }
}
